package com.ruoyi.caigou.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.caigou.domain.dto.BuyAskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.caigou.domain.BuyAsk;
import com.ruoyi.caigou.service.IBuyAskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购询价Controller
 * 
 * @author lvql
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/caigouxunjia")
public class BuyAskController extends BaseController
{
    @Autowired
    private IBuyAskService buyAskService;

    /**
     * 查询采购询价列表
     */
    @RequiresPermissions("caigou:caigouxunjia:list")
    @GetMapping("/list")
    public TableDataInfo list(BuyAsk buyAsk)
    {
        startPage();
        List<BuyAsk> list = buyAskService.selectBuyAskList(buyAsk);
        return getDataTable(list);
    }

    /**
     * 导出采购询价列表
     */
    @RequiresPermissions("caigou:caigouxunjia:export")
    @Log(title = "采购询价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyAsk buyAsk)
    {
        List<BuyAsk> list = buyAskService.selectBuyAskList(buyAsk);
        ExcelUtil<BuyAsk> util = new ExcelUtil<BuyAsk>(BuyAsk.class);
        util.exportExcel(response, list, "采购询价数据");
    }

    /**
     * 获取采购询价详细信息
     */
    @RequiresPermissions("caigou:caigouxunjia:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(buyAskService.selectBuyAskById(id));
    }

    /**
     * 获取采购询价详细信息
     */
//    @RequiresPermissions("caigou:caigouxunjia:query")
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfoPlus(@PathVariable("id") Integer id)
    {
        return success(buyAskService.selectBuyAskByIdPlus(id));
    }
    @GetMapping(value = "/info/byCode/{code}")
    public AjaxResult getInfoPlusByCode(@PathVariable("code") String code)
    {
        return success(buyAskService.selectBuyAskByCode(code));
    }
    /**
     * 新增采购询价
     */
    @RequiresPermissions("caigou:caigouxunjia:add")
    @Log(title = "采购询价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyAsk buyAsk)
    {

        return toAjax(buyAskService.insertBuyAsk(buyAsk));
    }

    /**
     * 新增采购询价 包含商品
     */
    @RequiresPermissions("caigou:caigouxunjia:add")
    @Log(title = "采购询价", businessType = BusinessType.INSERT)
    @PostMapping("/addPlus")
    public AjaxResult addPlus(@RequestBody BuyAskDTO buyAskDTO)
    {
        logger.info("BuyReqController addPlus buyAskDTO params： {}", buyAskDTO);
        if (buyAskDTO.getIdAndNumberList() == null|| buyAskDTO.getIdAndNumberList().isEmpty()){
            return AjaxResult.error("缺少商品参数");
        }
        return toAjax(buyAskService.insertBuyAskPlus(buyAskDTO));
    }

    /**
     * 修改采购询价
     */
    @RequiresPermissions("caigou:caigouxunjia:edit")
    @Log(title = "采购询价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyAsk buyAsk)
    {
        return toAjax(buyAskService.updateBuyAsk(buyAsk));
    }

    /**
     * 删除采购询价
     */
    @RequiresPermissions("caigou:caigouxunjia:remove")
    @Log(title = "采购询价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(buyAskService.deleteBuyAskByIds(ids));
    }
}
